home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 09 Racing and Sports AI / 02 Biasillo / Listing2.cpp < prev    next >
Encoding:
Text File  |  2001-12-09  |  1.1 KB  |  34 lines

  1. /* Copyright (C) Gari Biasillo, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Gari Biasillo, 2001"
  9.  */
  10.  
  11. // Once the AI has chosen its target, appropriate actions must 
  12. // be taken to resolve how to reach it. The AI aims to mimic user 
  13. // input so it must calculate the relative direction to travel and 
  14. // apply the appropriate steering.
  15.  
  16. float CCarAI::SteerToTarget(const vector3& dest)
  17. {
  18.     vector3 DestForward, cp;
  19.  
  20.     DestForward = dest - m_Pos;
  21.     DestForward.y = 0.0f;
  22.     DestForward.normalize();
  23.  
  24.     // Compute the sine between current & destination.
  25.     cp = CrossProduct(m_Forward, DestForward);
  26.     float steer = cp.magnitude() * m_SteerConvert;
  27.  
  28.     // Steer left or right ?
  29.     if (cp.y > 0.0f)  {steer = -steer;}
  30.     steer = Clamp(steer, -1.0f, 1.0f);
  31.  
  32.     return (steer);
  33. }
  34.